package com.university.trade.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.university.trade.entity.Blogger;
import com.university.trade.exception.ValidateCodeErrorException;
import com.university.trade.service.BloggerService;
import com.university.trade.util.CryptographyUtil;
import com.university.trade.util.PropertiesUtil;
import com.university.trade.util.RequestUtil;
import com.university.trade.util.StringUtil;

/**
 * 博主Controller层
 * @author java1234_小锋
 *
 */
@Controller
@RequestMapping("/blogger")
public class BloggerController {

	@Resource
	private BloggerService bloggerService;
	
	/**
	 * 用户登录
	 * @param blogger
	 * @param request
	 * @return
	 */
	@RequestMapping("/login")
	public String login(Blogger blogger,HttpServletRequest request){
		Subject subject=SecurityUtils.getSubject();
		PropertiesUtil systemProperties = new PropertiesUtil("/systemConfig.properties");
		String system_second_level_encrypt_str = systemProperties.getProperty("system_second_level_encrypt_str");
		UsernamePasswordToken token=new UsernamePasswordToken(blogger.getUserName(), CryptographyUtil.md5(blogger.getPassword(), system_second_level_encrypt_str));
		try{
			String validateCode = RequestUtil.paramStringParse(request.getParameter("validateCode"));
			String system_validateCodeLength = systemProperties.getProperty("system_validateCodeLength");
			if(StringUtil.isEmpty(validateCode)){
				throw new ValidateCodeErrorException("验证码不能为空！");
			}else if(validateCode.length() != Integer.valueOf(system_validateCodeLength)){
				throw new ValidateCodeErrorException("验证码格式不正确！");
			}else{
				String session_validateCode = "";
				String system_open_test = systemProperties.getProperty("system_open_test");
				if("T".equals(system_open_test)){
					session_validateCode = "1234";//测试环境默认验证码为1234
				}else{
					session_validateCode = (String) request.getSession().getAttribute("session_validateCode");
				}
				if(!validateCode.equals(session_validateCode)){
					throw new ValidateCodeErrorException("验证码不正确，请重试！");
				}
			}
			subject.login(token); // 登录验证
			return "redirect:/admin/main.jsp";
		}catch(ValidateCodeErrorException e){
			e.printStackTrace();
			request.setAttribute("blogger", blogger);
			request.setAttribute("errorInfo", e.getMessage());
			return "login";
		}catch(Exception e){
			e.printStackTrace();
			request.setAttribute("blogger", blogger);
			request.setAttribute("errorInfo", "用户名或密码错误！");
			return "login";
		}
	}
	
	/**
	 * 查找博主信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/aboutMe")
	public ModelAndView aboutMe()throws Exception{
		ModelAndView mav=new ModelAndView();
		mav.addObject("blogger",bloggerService.find());
		mav.addObject("mainPage", "foreground/blogger/info.jsp");
		mav.addObject("pageTitle","关于博主_Java开源博客系统");
		mav.setViewName("mainTemp");
		return mav;
	}
}
